package com.maya.medicalwastes.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;

import androidx.fragment.app.Fragment;


/**
 * @Creator gzl
 * @date 15/11/16 10:45
 * @Description <p/> 常用系统工具类
 * <p/>
 * getRawSize 转换数据类型 dip2dp
 * getStatusHeight 获取系统通知栏高度
 * isFlymeOs4x 判断系统是是否是魅族
 */
public class MyUtils {
    /**
     * *
     * 转换数值类型
     *
     * @param context
     * @param unit    数据类型 dp sp等
     * @param value   转换前的数值
     * @return 转换后的数值
     */
    public static float getRawSize(Context context, int unit, float value) {
        Resources res = context.getResources();
        return TypedValue.applyDimension(unit, value, res.getDisplayMetrics());
    }
    /**
     * intent跳转
     *
     * @param activity 原activity
     * @param cls      跳转activity
     * @param bundle   传递的bundle
     */
    public static void IntentToOther(Context activity, Class<?> cls,
                                     Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(activity, cls);
        if (bundle != null)
            intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    /**
     * app关闭状态跳转自定义页面
     *
     * @param activity 原activity
     * @param cls      跳转activity
     * @param bundle   传递的bundle
     */
    public static void IntentToNew(Context activity, Class<?> cls,
                                   Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(activity, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        if (bundle != null)
            intent.putExtras(bundle);
        activity.startActivity(intent);
    }
    /**
     * app关闭状态跳转自定义页面
     *
     * @param activity 原activity
     * @param cls      跳转activity
     * @param bundle   传递的bundle
     */
    public static void IntentToNewBySingleTaskMode(Context activity, Class<?> cls,
                                   Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(activity, cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        if (bundle != null)
            intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    /**
     * 带result的activity跳转
     *
     * @param activity 原activity
     * @param cls      跳转activity
     * @param bundle   传递的bundle
     * @param code     requestCode
     */
    public static void IntentToOtherForResult(Object activity, Class<?> cls,
                                              Bundle bundle, int code) {
        Intent intent = new Intent();
        if (bundle != null)
            intent.putExtras(bundle);
        if (activity instanceof Activity) {
            intent.setClass((Activity) activity, cls);
            ((Activity) activity).startActivityForResult(intent, code);
        } else if (activity instanceof Fragment) {
            intent.setClass(((Fragment) activity).getActivity(), cls);
            ((Fragment) activity).startActivityForResult(intent, code);
        }
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
